#!/bin/bash

testRaft(){
	go test -race -run $1 > raft.txt
	res=$(awk '$0 ~ /ok/ {print $0}' raft.txt)
	leader_reject=$(awk '$0 ~ /leader rejected Start/ {print $0}' raft.txt)
	fail_agree=$(awk '$0 ~ /failed to reach agreement/ {print $0}' raft.txt)
	echo $res
	#echo $leader_reject
	#echo $fail_agree
	if [ -n "${res}" ]
	then
		echo "$1 Passed"
		#rm *.txt
		return 1
	else
		if [ -n "${leader_reject}" -a -z "${fail_agree}" ]
		then
			echo "$1 Passed"
        		#rm *.txt
        		return 1
		else
			echo "$1 Failed"
			return 0
		fi
	fi
}
res=1
i=0
true_res=1
max_loop=$2
test_pattern=$1
echo "Test Pattern:$test_pattern"
export "GOPATH=$PWD"
while [ "$res" -eq "$true_res" -a "$i" -lt "$max_loop" ]
do
	testRaft "$test_pattern"
	res=$?
	echo "res:$res,loop:$i"
	let "i++"
	sleep 2s
done
